package com.burgeon.backend.SHOPBACKEND.controller;

import com.alibaba.fastjson.JSONObject;
import com.burgeon.backend.SHOP.entity.VipEmployeeVo;
import com.burgeon.backend.SHOPBACKEND.entity.ShopKf;
import com.burgeon.backend.SHOPBACKEND.entity.WeiKF;
import com.burgeon.backend.SHOPBACKEND.service.WeChatKFService;
import com.burgeon.backend.Util.menu.CommonUtil;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

@RequestMapping("WeKF")
@RestController
@CrossOrigin
public class WeChatKFController extends BaseController {
    @Autowired
    private WeChatKFService weChatKFService;

//    @RequestMapping("getKFAll")
//    public ResponseResult<ArrayList<WeiKF>> findAll(String token){
//        ArrayList<WeiKF> data = weChatKFService.findWeKFAll(token);
//        return new ResponseResult<>(SUCCESS,data);
//    }

    @RequestMapping("add")
    public ResponseResult<Void> addKF(String token,String kfAccount,String nickName,String password){
         weChatKFService.addKF(token,kfAccount,nickName,password);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("bind")
    public ResponseResult<Void> bindKF(String token,String kfAccount,String kfWx){
        weChatKFService.bindKF(token,kfAccount,kfWx);
        return new ResponseResult<>(SUCCESS);
    }

//    上传头像
    @RequestMapping("updateHeadImg")
    public ResponseResult<Void> updateHeadImg(MultipartFile file, String kfAccount, String token){
        weChatKFService.addCustomer(file,kfAccount,token);
        return new ResponseResult<>(SUCCESS);
    }

    //修改客服昵称
    @RequestMapping("updateKF")
    public ResponseResult<Void> updateKF(String token,String kfAccount,String nickName){
        weChatKFService.updateKF(token, kfAccount, nickName);
        return new ResponseResult<>(SUCCESS);
    }

    //删除客服
    @RequestMapping("delKF")
    public ResponseResult<Void> delKF(String token,String kfAccount){
        weChatKFService.DelKF(token, kfAccount);
        return new ResponseResult<>(SUCCESS);
    }

    //客服绑定导购，获取openid,，生成二维码
    @RequestMapping("creatCode")
    public ResponseResult<String> creatWeCodeUrl(String token) {
        String data = weChatKFService.creatWeCodeUrl(token);
        return new ResponseResult<>(SUCCESS,data);
    }

//    //扫码绑定
//    @RequestMapping(value = "bindKfOpenid", method = RequestMethod.GET)
//    @ResponseBody
//    public ResponseResult<Void> bindKfOpenid(HttpServletRequest request,String kfId){
//
//        String code = request.getParameter("code");
//       // String kfId = request.getParameter("kfId");
//        System.err.println(code);
//        System.err.println(kfId);
//
//        JSONObject jsonDate = CommonUtil.getOpenId(code);
//        //  System.err.println(jsonDate);
//        String openId = "";
//        if (null == jsonDate.get("errcode")) {
//            openId = jsonDate.getString("openid");
//            System.err.println(openId);
//        }
//        System.err.println(openId);
//
//        return new ResponseResult<>(SUCCESS);
//    }

    /***************************绑定客服****************************************/



    @RequestMapping("bindKf")
    public ResponseResult<Integer> bind(String openid,String token){
//        String openid = session.getAttribute("app_openid").toString();
//        String token = session.getAttribute("token").toString();
        System.err.println("openid===="+openid);
        System.err.println(token);
        Integer data = weChatKFService.insertOpenId(openid,token);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     *获取所有客服
     */
    @RequestMapping("getKFAll")
    public ResponseResult<ArrayList<ShopKf>> findAll(String token) throws UnsupportedEncodingException {
        ArrayList<ShopKf> data = weChatKFService.findWeKF(token);
        System.err.println(data);
        return new ResponseResult<>(SUCCESS,data);
    }

    /**
     * 根据id删除客服
     * @param token
     * @param id
     * @return
     */
    @RequestMapping("DeleteKFById")
    public ResponseResult<Integer> DeleteKFById(String token,String id) {
        Integer data = weChatKFService.deleteKfById(token,id);
        return new ResponseResult<>(SUCCESS,data);
    }





}
